package com.briup.smartcity.web.controller;

import com.briup.smartcity.aop.Logging;
import com.briup.smartcity.bean.Article;
import com.briup.smartcity.bean.extend.ArticleExtend;
import com.briup.smartcity.service.IArticleService;
import com.briup.smartcity.utils.Result;
import com.briup.smartcity.utils.ResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//控制器
@Api(tags = "资讯信息模块")
@RestController
@RequestMapping("/auth/article")
public class ArticleController {
    @Autowired
    private IArticleService service;
    @ApiOperation("通过id获取资讯详情")
    @GetMapping("/findById")
    public Result findById(int id){
        ArticleExtend byIdWithCategoryAndUser = service.findByIdWithCategoryAndUser(id);
        return ResultUtil.success(byIdWithCategoryAndUser);
    }


    @ApiOperation("通过标题、状态、栏目分页查找资讯信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页数",dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "每页数量",dataType = "int"),
            @ApiImplicitParam(name = "title",value = "题目",dataType = "String"),
            @ApiImplicitParam(name = "status",value = "状态",dataType = "int"),
            @ApiImplicitParam(name = "categoryId",value = "编号",dataType = "int")
    })
    @GetMapping("/page/queryWithCategoryAndUser")
    public Result pageInfoQuery(int pageNum, int pageSize,String title,Integer status,Integer categoryId){
        PageInfo<ArticleExtend> articleExtendPageInfo = service.pageQueryArticleWithCategoryAndUser(pageNum, pageSize, title, status, categoryId);
        return ResultUtil.success(articleExtendPageInfo);
    }


    @ApiOperation("通过id删除资讯")
    @Logging()
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",dataType = "int")
    })
    @DeleteMapping ("/deleteById")
    public Result deleteById(int id){
        service.deleteById(id);
        return ResultUtil.success();
    }


    @ApiOperation("保存或更新资讯信息")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(Article article){
        service.saveOrUpdate(article);
        return ResultUtil.success();
    }


    @ApiOperation("更新资讯状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",dataType = "int"),
            @ApiImplicitParam(name = "status",value = "状态,0-待审核，1-不通过，2-通过，3-推荐",dataType = "int")
    })
    @PutMapping("/changeStatus")
    public Result changeStatus(int id,int status){
        service.changeStatus(id, status);
        return ResultUtil.success();
    }
}
